﻿namespace CRMG.Container.Source.OpManager
{
    using CRMG.Container.Source.BO;
    using CRMG.Container.Source.DL;
    using CRMG.Container.Source.Variables;
    using CRMG.ResourceManagement.Resources;
    using CRMG.UserManagement;
    using Guru.ErrorHandling;
    using System;
    using System.Data;

    public class TrainManager
    {
        UserManager pUsrMan;
        public TrainManager()
        {
            pUsrMan = new UserManager();
        }

        #region [ Trainers ]

        public DataTable GetTrainers()
        {
            try
            {
                return pUsrMan.GetTrainerUsers();
            }
            catch (Exception exc)
            {
                LogEntry logger = new LogEntry(AppVariables.CurrentUser.OBJID)
                {
                    ErrorCode = 2032,
                    Title = Rc.GetString(2032)
                };
                throw new GuruException(exc, logger);
            }
        }

        #endregion


        #region [ TrainClass ]

        public DataTable GetTrainClasses()
        {
            try
            {
                TrainClassDL trainclassDL = new TrainClassDL();
                return trainclassDL.GetTrainClasses();
            }
            catch (Exception exc)
            {
                LogEntry logger = new LogEntry(AppVariables.CurrentUser.OBJID)
                {
                    ErrorCode = 2026,
                    Title = Rc.GetString(2026)
                };
                throw new GuruException(exc, logger);
            }
        }

        public int AddTrainClass(TrainClass trainClass)
        {
            try
            {
                int retInt = -1;
                TrainClassDL tcDL = new TrainClassDL();
                retInt = tcDL.InsertAndGetId(trainClass);
                return retInt;
            }
            catch (Exception exc)
            {
                LogEntry logger = new LogEntry(AppVariables.CurrentUser.OBJID)
                {
                    ErrorCode = 2027,
                    Title = Rc.GetString(2027)
                };
                throw new GuruException(exc, logger);
            }
        }

        public void UpdateTrainClass(TrainClass trainClass)
        {
            try
            {
                TrainClassDL tcDL = new TrainClassDL();
                tcDL.Update(trainClass);
            }
            catch (Exception exc)
            {
                LogEntry logger = new LogEntry(AppVariables.CurrentUser.OBJID)
                {
                    ErrorCode = 2028,
                    Title = Rc.GetString(2028)
                };
                throw new GuruException(exc, logger);
            }
        }

        #endregion


        #region [ Train ]

        public DataTable GetTrains()
        {
            try
            {
                TrainDL trDL = new TrainDL();
                return trDL.GetTrains();
            }
            catch (Exception exc)
            {
                LogEntry logger = new LogEntry(AppVariables.CurrentUser.OBJID)
                {
                    ErrorCode = 2021,
                    Title = Rc.GetString(2021)
                };
                throw new GuruException(exc, logger);
            }
        }

        public int AddTrain(Train pTrain)
        {
            try
            {
                int retInt = -1;
                TrainDL _trainDL = new TrainDL();
                retInt = _trainDL.InsertAndGetId(pTrain);
                return retInt;
            }
            catch (Exception exc)
            {
                LogEntry logger = new LogEntry(AppVariables.CurrentUser.OBJID)
                {
                    ErrorCode = 2022,
                    Title = Rc.GetString(2022)
                };
                throw new GuruException(exc, logger);
            }
        }

        public void UpdateTrain(Train pTrain)
        {
            try
            {
                TrainDL _trainDL = new TrainDL();
                _trainDL.Update(pTrain);
            }
            catch (Exception exc)
            {
                LogEntry logger = new LogEntry(AppVariables.CurrentUser.OBJID)
                {
                    ErrorCode = 2023,
                    Title = Rc.GetString(2023)
                };
                throw new GuruException(exc, logger);
            }
        }

        public Train GetTrainById(int trainId)
        {
            try
            {
                TrainDL _trainDL = new TrainDL();
                return _trainDL.GetTrainById(trainId);
            }
            catch (Exception exc)
            {
                LogEntry logger = new LogEntry(AppVariables.CurrentUser.OBJID)
                {
                    ErrorCode = 2092,
                    Title = Rc.GetString(2092)
                };
                throw new GuruException(exc, logger);
            }
        }

        public Train GetTrainByVendorAndName(string name, int vendorId)
        {
            try
            {
                TrainDL _trainDL = new TrainDL();
                return _trainDL.GetTrainByVendorAndName(name, vendorId);
            }
            catch (Exception exc)
            {
                LogEntry logger = new LogEntry(AppVariables.CurrentUser.OBJID)
                {
                    ErrorCode = 2093,
                    Title = Rc.GetString(2093)
                };
                throw new GuruException(exc, logger);
            }
        }


        #endregion


        #region [ Get Student Trains ]

        public DataTable GetStudentTrains(int studentId)
        {
            try
            {
                StudentTrainDL strnDL = new StudentTrainDL();
                return strnDL.GetStudentTrains(studentId);
            }
            catch (Exception exc)
            {
                LogEntry logger = new LogEntry(AppVariables.CurrentUser.OBJID)
                {
                    ErrorCode = 2014,
                    Title = Rc.GetString(2014)
                };
                throw new GuruException(exc, logger);
            }
        }

        public int AddStudentTrain(StudentTrain studentTrain)
        {
            try
            {
                StudentTrainDL stdntDL = new StudentTrainDL();
                return stdntDL.InsertAndGetId(studentTrain);
            }
            catch (Exception exc)
            {
                LogEntry logger = new LogEntry(AppVariables.CurrentUser.OBJID)
                {
                    ErrorCode = 2019,
                    Title = Rc.GetString(2019)
                };
                throw new GuruException(exc, logger);
            }
        }

        #endregion


        #region [ Available Trains ]

        public DataTable GetAvailableTrainsOfStudent(int studentId)
        {
            try
            {
                StudentTrainDL avDL = new StudentTrainDL();
                return avDL.GetAvailableTrainsOfStudent(studentId);
            }
            catch (Exception exc)
            {
                LogEntry logger = new LogEntry(AppVariables.CurrentUser.OBJID)
                {
                    ErrorCode = 2102,
                    Title = Rc.GetString(2102)
                };
                throw new GuruException(exc, logger);
            }
        }

        public void UpdateAvailableTrain(AvailableTrain pAvailableTrain)
        {
            try
            {
                AvailableTrainDL avtrainDL = new AvailableTrainDL();
                avtrainDL.InsertAndGetId(pAvailableTrain);
            }
            catch (Exception exc)
            {
                LogEntry logger = new LogEntry(AppVariables.CurrentUser.OBJID)
                {
                    ErrorCode = 2037,
                    Title = Rc.GetString(2037)
                };
                throw new GuruException(exc, logger);
            }
        }

        public int AddAvailableTrain(AvailableTrain pAvailableTrain)
        {
            try
            {
                int avtrainId = -1;
                AvailableTrainDL avtrainDL = new AvailableTrainDL();
                avtrainId = avtrainDL.InsertAndGetId(pAvailableTrain);
                return avtrainId;
            }
            catch (Exception exc)
            {
                LogEntry logger = new LogEntry(AppVariables.CurrentUser.OBJID)
                {
                    ErrorCode = 2036,
                    Title = Rc.GetString(2036)
                };
                throw new GuruException(exc, logger);
            }
        }

        public AvailableTrain GetAvailableTrainById(int availableTrainId)
        {
            try
            {
                AvailableTrainDL trainDL = new AvailableTrainDL();
                return trainDL.GetAvailableTrainById(availableTrainId);
            }
            catch (Exception exc)
            {
                LogEntry logger = new LogEntry(AppVariables.CurrentUser.OBJID)
                {
                    ErrorCode = 2031,
                    Title = Rc.GetString(2031)
                };
                throw new GuruException(exc, logger);
            }
        }

        public DataTable GetAvailableTrains()
        {
            try
            {
                AvailableTrainDL trainDL = new AvailableTrainDL();
                return trainDL.GetAvailableTrains();
            }
            catch (Exception exc)
            {
                LogEntry logger = new LogEntry(AppVariables.CurrentUser.OBJID)
                {
                    ErrorCode = 2015,
                    Title = Rc.GetString(2015)
                };
                throw new GuruException(exc, logger);
            }
        }

        #endregion


        #region [ Train Types ]

        public DataTable GetTrainTypes()
        {
            try
            {
                TrainTypeDL trTypeDL = new TrainTypeDL();
                return trTypeDL.GetTable(new TrainType());
            }
            catch (Exception exc)
            {
                LogEntry logger = new LogEntry(AppVariables.CurrentUser.OBJID)
                {
                    ErrorCode = 2020,
                    Title = Rc.GetString(2020)
                };
                throw new GuruException(exc, logger);
            }
        }
        #endregion
    }
}
